package org.sakai.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
public class PayService {
    private final String APP_ID = "9021000139637661";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCOiuGYkCuNs4xbvdFVp6dFZuBEeYP0mVTSUoocnrdpQSY0tp1AvnLoHsRyVb2bO8pay7hzdDZ3PyvYF1IShgXrSCvuCAnfrewiapPzvkldX2bAwprYX0TdUuxQW8EYvmH5wx8fyvkz6iO8vlrRnlyn8aQQ/ldfUR248YbMY3Icf+WgtNUcZoju/x7F+JxJoJ+Zq9CnDuvFNw0uMXTary9nXVkSVJanKnIj50TCkvkZD5FqI3kHqQOca/cf2c52mCawtbNQP0ObcEo2xoXH8ugemEwcT5sm0ZpLdPNOHoAAwg2WgsviHJO1Det/R53DyIVU+rMdlX4TGAyu8n25R5UHAgMBAAECggEALS7sz9VzWNl/Nl13C7W3GhLAKDR0lYY9EuZDDUtqTBzwpWj1c95qwhVuo+d10M0lGZ7V+xLtaONHXJoX0/NT0P7xneRCbPrOjSJHf83jp4tkINoPB2M7iwSMD/TlwpgmqmslZIiMcd5YMa4NT8DpEftQzGoy++3UhBlbZ1Jmi42tJtNhn/OsMPc5MjAHVnGRT2mDbWEW64JX356BrVYqJKdXb+dpR5FHMV9XVsLK8u6DaZV64bRJ+7TEkh6Ctea4yjDsCH9SF/1oxC6Fth59Dt0XaALJ+FnbdqOYa+uVQqovsM9QWK+eYmGwsCF2NkrZ7ptaiYN6vI9pdgZVdEIpcQKBgQDWvbyVeIRfMJjuclqT0ab9rxQ8ytx9mYItfJddx5Z8M1euYEbUaCCZIuPppQSt9z2mmqQzRtDYBNkXReI7p47KNqmvgHigU8fnzbnQARUUXStI6VSfc8PAugQsAW9as/kaz+CrWG9aEpaBNF6G6zoZEnm+qYBXuuVL78CYAhUbhQKBgQCp7frhU9EOjRbgFfybOxzOPig9icYU/lE8opAkSXF7ZObh79se9AIyrvWYNkyKgDi2VmyPbko2SeEyVNClm9QqjpSs3fSYfk3yvlWQxmVRQxAeH7RlkL+sqBocA7TsSk1xH+oBe80g9rsa6NEasZotJcZrHTquz1xa/VrqPkNWGwKBgAbXdLKubEk8GyM9sXUUMOXUNijZrbyyr1sTpR7vxPOgDyMXHiqWRrOaUPiYsVV5kxVDyAd6Mh0vylrbdyG/bOU0aKOWzEEPakcQXuMppapIbaCdDn0mCw6Q3qEoSovxP/aCuP28i4+rF/B14q0ZwXgbBpX0gIpLAhMxSEmPh2yhAoGBAIC5OrvoD0xzimmTnjhFDiwZn8gGDtwmTomdJ42laOatZ8ytMpL/Co80Q+za7R5ZxRwy2afJR0RasNG8uBVLtuvJ+Ev/EuNVglyo3A6Ai42EPLhlEfWCMgBrkuO31xzLqGBSaaRYEOqwnMnkpUoQUZdzrxvDF4oGoJJ4q58YLVATAoGAbC2Etplh0yCEpnvEtjUpfDKoOFlWAvoAX79dfuZiiqZn7J/r8Rfw9MtzAqGom4F2UsnbC08ok3uxA8KPApXKC4cS2IVQvOsfMobpWiaT8LhIcHqSc3sEmyjX/GbM+ZCdRDsgIBEBktCEt5HKnjEgd95BrAcAvJqSpj497WRTZog=";
    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjKJYXMG1vIytr/YAkNIG6yjj1ueIUZhDL8LdXbXHaJAceWGkr91yQrhdLg6dGro64AQfKCVjFuhWk5BcmvOaqT9LGerIcC80pqhLNyH9Kd4Wb5qPYV0KxKHDT7dw5dQatMWypypzaMl/XI0wpmzPkfAePJYJ+CLSlf4ymPtubJFbmmKCVcAAFbC3kjO64RSN6n8OeU2RX767K8c+rD9hGywjLY1x1m9HLy+DaCnhVPXE6qLRuEu/xdP5CXQGLTlA7Eu5p0ydsLhSUIeDo63VsvbvQnBX0txK//T1+O3dXzSdk02IID61u/sy+0Zx9FBZxgzrWXPIt6m2yz36LMcyCwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://xg-4.frp.one:15550/pay/consult/payFinished";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:80/room";

    //支付宝官方提供的接口
    public String sendRequestToAlipay(String orderId, Double totalAmount) {
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY,
                    FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            alipayRequest.setReturnUrl(RETURN_URL + "?orderId=" + orderId);
            alipayRequest.setNotifyUrl(NOTIFY_URL + "/" + orderId);

            String body = "";
            alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderId + "\","
                    + "\"total_amount\":\"" + totalAmount + "\","
                    + "\"subject\":\"医疗付费\","
                    + "\"body\":\"" + body + "\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

            String result = alipayClient.pageExecute(alipayRequest).getBody();
            return result;

        } catch (AlipayApiException e) {
            // 可选：返回错误信息给前端
            return "支付请求失败，请稍后重试";
        }
    }
}
